#include "ScriptedPch.h"
#include "ScriptSystem.h"

const char * WHISPER_NOT_DONATION  = "ERROR: That item is not worth any donation marks!";
const char * WHISPER_NOT_EXIST     = "ERROR: That item does not exist!";
const char * WHISPER_NOT_HAVE_ITEM = "ERROR: You do not have that item in your inventory.";

uint32 DONATION_MARK = 555555;
uint32 GOSSIP_CHATID = 555555; //TODO

bool GossipHello_npc_donation_return(Player *pl, Creature *_c)
{
    if(pl->isInCombat())
        pl->CLOSE_GOSSIP_MENU();

    pl->ADD_GOSSIP_ITEM_EXTENDED(GOSSIP_ICON_CHAT, "Exchange Donation Item for Donor Marks", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+1, "", 0, true);
    pl->PlayerTalkClass->SendGossipMenu(GOSSIP_CHATID, _c->GetGUID());

    return true;
}

bool GossipSelectWithCode_npc_donation_return_codebox(Player* pl, Creature* _c, uint32 uiSender, uint32 uiAction, const char* sCode)
{
    if(uiSender != GOSSIP_SENDER_MAIN)
        return true;

    switch(uiAction)
    {
        case GOSSIP_ACTION_INFO_DEF+1:
        {
            QueryResult_AutoPtr result = WorldDatabase.PQuery("SELECT `entry`, `donation_value` FROM item_template WHERE name LIKE '%s';", sCode);
            if(!result)
            {
                _c->MonsterWhisper(WHISPER_NOT_EXIST, pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }

            Field * fields = result->Fetch();
            uint64 ientryid = fields[0].GetUInt64();
            uint64 dWorth = fields[1].GetUInt64();

            if(!dWorth)
            {
                _c->MonsterWhisper(WHISPER_NOT_DONATION, pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }
                
            result = CharacterDatabase.PQuery("SELECT * FROM character_inventory WHERE guid='%u' AND item_template='%u';", pl->GetGUIDLow(), ientryid);
            if(!result)
            {
                _c->MonsterWhisper(WHISPER_NOT_HAVE_ITEM, pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }

            ItemPosCountVec dest;
            uint8 msg = pl->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, DONATION_MARK, 1);
            if (msg == EQUIP_ERR_OK)
            {
                Item* item = pl->StoreNewItem(dest,DONATION_MARK,true);
                if (item)
                {
                    pl->SendNewItem(item, dWorth ,false, true);
                    pl->DestroyItemCount(ientryid, 1, true, false);
                }
            }
            else
                pl->SendEquipError(msg,NULL,NULL);

            pl->CLOSE_GOSSIP_MENU();
            break;
        }
    }

    return true;
}

void AddSC_donation_return()
{
    Script *newscript;

    newscript = new Script;
    newscript->Name="donation_return";
    newscript->pGossipHello =           &GossipHello_npc_donation_return;
    newscript->pGossipSelectWithCode =  &GossipSelectWithCode_npc_donation_return_codebox;
    newscript->RegisterSelf();
}




